package com.bianguo.android.apushdemo.view.activity;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bianguo.android.apushdemo.R;
import com.bianguo.android.apushdemo.adapter.MapAdapter;
import com.bianguo.android.apushdemo.utils.MyLog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

public class MapActivity extends BaseActivity {

    @BindView(R.id.map_content_tv)
    TextView contentLayout;
    @BindView(R.id.map_shrink_tv)
    TextView shrinkTv;
    @BindView(R.id.map_recycler)
    RecyclerView mRecycler;
    @BindView(R.id.map_bottom_layout)
    LinearLayout shrinkLayout;

    private ArrayList<String> list;
    private MapAdapter mapAdapter;
    private LinearLayoutManager layoutManager;
    private int mRecyclerHeight;

    @Override
    public int layoutId() {
        return R.layout.activity_map;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        layoutManager = new LinearLayoutManager(mContext);
        layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRecycler.setLayoutManager(layoutManager);
        list = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            list.add("item " + i);
        }
        mapAdapter = new MapAdapter(mContext, list);
        mRecycler.setAdapter(mapAdapter);
    }

    @OnClick({R.id.map_shrink_tv})
    public void onClickBk(View view) {
        switch (view.getId()) {
            case R.id.map_shrink_tv:
                shrinkTv.setEnabled(false);
                int contentLayoutHeight = contentLayout.getHeight();
                if (mRecyclerHeight == 0) {
                    mRecyclerHeight = mRecycler.getHeight();
                }
                String str = shrinkTv.getText().toString().trim();
                if (TextUtils.equals("收缩", str)) {
                    shrink(contentLayoutHeight + mRecyclerHeight);
                } else if (TextUtils.equals("拉伸", str)) {
                    shrink(contentLayoutHeight - mRecyclerHeight);
                }
                break;
        }
    }

    private void shrink(int height) {
        ValueAnimator animator = createDropAnimator(contentLayout, contentLayout.getHeight(), height);
        animator.setDuration(500);
        animator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                String str = shrinkTv.getText().toString().trim();
                if (TextUtils.equals("收缩", str)) {
                    shrinkTv.setText("拉伸");
                } else if (TextUtils.equals("拉伸", str)) {
                    shrinkTv.setText("收缩");
                    mapAdapter.notifyDataSetChanged();
                }
                shrinkTv.setEnabled(true);
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });
        animator.start();
    }

    private ValueAnimator createDropAnimator(final View view, int start, int end) {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int value = (int) animation.getAnimatedValue();
                ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
                layoutParams.height = value;
                view.setLayoutParams(layoutParams);
            }
        });
        return valueAnimator;
    }
}
